<?php

namespace app\admin\model\note;

use app\admin\model\facrm\Customer;
use app\admin\model\facrm\customer\Contacts;
use app\admin\model\User;
use think\Model;


class Order extends Model
{

    

    

    // 表名
    protected $name = 'note_order';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';
    protected $deleteTime = 'delete_time';

    // 追加属性
    protected $append = [
        'start_time_text',
        'end_time_text',
        'is_memory_text',
        'create_time_text',
        'update_time_text',
        'delete_time_text'
    ];
    

    
    public function getIsMemoryList()
    {
        return ['0' => __('Is_memory 0'), '1' => __('Is_memory 1')];
    }


    public function getStartTimeTextAttr($value, $data)
    {
        $value = $value ?: ($data['start_time'] ?? '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getEndTimeTextAttr($value, $data)
    {
        $value = $value ?: ($data['end_time'] ?? '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getIsMemoryTextAttr($value, $data)
    {
        $value = $value ?: ($data['is_memory'] ?? '');
        $list = $this->getIsMemoryList();
        return $list[$value] ?? '';
    }


    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ?: ($data['create_time'] ?? '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUpdateTimeTextAttr($value, $data)
    {
        $value = $value ?: ($data['update_time'] ?? '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getDeleteTimeTextAttr($value, $data)
    {
        $value = $value ?: ($data['delete_time'] ?? '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setStartTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setEndTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setUpdateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setDeleteTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

	public function user(){
		return $this->belongsTo(User::class, 'uid', 'uid');
	}

	public function customer(){
		return $this->belongsTo(Customer::class, 'customer_id', 'id');
	}

	public function contact(){
		return $this->belongsTo(Contacts::class, 'contact_id', 'id');
	}


}
